package models

import (
	"time"

	"gorm.io/gorm"
)

// Goods 商品库
type Goods struct {
	GoodsID int `gorm:"primary_key" json:"goods_id"` // 商品ID

	GoodsName     string    `json:"goods_name"`     // 商品名称
	GoodsSpec     string    `json:"goods_spec"`     // 商品规格
	GoodsNo       string    `json:"goods_no"`       // 商品编码
	CostPrice     int       `json:"cost_price"`     // 成本价
	SupplyPrice   int       `json:"supply_price"`   // 供应价
	SellingPrice  int       `json:"selling_price"`  // 卖货价
	StreamerId    int       `json:"streamer_id"`    // 主播ID
	GoodsImg      string    `json:"goods_img"`      // 商品图片
	ZytGoodsId    int       `json:"zyt_goods_id"`   // 找源头商品ID
	CreateTime    time.Time `json:"create_time"`    // 创建时间
	UpdateTime    time.Time `json:"update_time"`    // 更新时间
	InstitutionId int       `json:"institution_id"` // 机构ID
	GoodsNum      int       `json:"goods_num"`      // 商品数量
	UserId        int       `json:"user_id"`        // 创建人

	Streamer Streamer
	User     User
	GoodsTag []GoodsTag
}

// TableName 表名称
func (*Goods) TableName() string {
	return "goods"
}

// GetGoods 查询多条数据
func GetGoods(fields []string, maps map[string]interface{}) ([]*Goods, error) {
	var address []*Goods
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&address).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return address, nil
}

func GetGoodsPage(fields []string, maps map[string]interface{}, pageNum int, pageSize int, order string) ([]*Goods, error) {
	var Goods []*Goods
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Preload("Streamer").Preload("User").Preload("GoodsTag.Tag").Where(cond, vals...).Offset(pageNum).Limit(pageSize).Order(order).Find(&Goods).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return Goods, nil
}

// GetGoodsInfo 查询多条数据
func GetGoodsInfo(fields []string, maps map[string]interface{}) (*Goods, error) {
	var Goods Goods
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&Goods).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return &Goods, nil
}

// AddGoods 插入数据
func AddGoodsDB(data map[string]interface{}, db *gorm.DB) (int, error) {
	address := Goods{
		InstitutionId: data["institution_id"].(int),
		GoodsName:     data["goods_name"].(string),
		GoodsSpec:     data["goods_spec"].(string),
		GoodsNo:       data["goods_no"].(string),
		SupplyPrice:   data["supply_price"].(int),
		SellingPrice:  data["selling_price"].(int),
		GoodsImg:      data["goods_img"].(string),
		GoodsNum:      data["goods_num"].(int),
		UserId:        data["user_id"].(int),
	}
	result := db.Select("institution_id", "user_id", "goods_name", "goods_spec", "goods_no", "supply_price", "selling_price", "goods_img", "goods_num").Create(&address)
	if err := result.Error; err != nil {
		return 0, err
	}
	return address.GoodsID, nil
}

// Goods 商品库
type GoodsAll struct {
	GoodsName     string `json:"goods_name"`     // 商品名称
	GoodsSpec     string `json:"goods_spec"`     // 商品规格
	GoodsNo       string `json:"goods_no"`       // 商品编码
	CostPrice     int    `json:"cost_price"`     // 成本价
	SupplyPrice   int    `json:"supply_price"`   // 供应价
	SellingPrice  int    `json:"selling_price"`  // 卖货价
	StreamerId    int    `json:"streamer_id"`    // 主播ID
	GoodsImg      string `json:"goods_img"`      // 商品图片
	ZytGoodsId    int    `json:"zyt_goods_id"`   // 找源头商品ID
	InstitutionId int    `json:"institution_id"` // 机构ID
	GoodsNum      int    `json:"goods_num"`      // 商品数量
}

// TableName 表名称
func (*GoodsAll) TableName() string {
	return "goods"
}

// AddGoods 插入数据
func AddGoodsAll(goodsList []GoodsAll) (int, error) {
	result := db.CreateInBatches(goodsList, len(goodsList))
	if result.Error != nil {
		panic("failed to insert batch: " + result.Error.Error())
	}
	return 1, nil
}

// EditGoods modify a single Goods
func EditGoods(where map[string]interface{}, data interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Model(&Goods{}).Where(cond, vals...).Updates(data).Error; err != nil {
		return err
	}
	return nil
}

// EditGoods modify a single Goods
func EditGoodsDB(where map[string]interface{}, data interface{}, db *gorm.DB) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Model(&Goods{}).Where(cond, vals...).Updates(data).Error; err != nil {
		return err
	}
	return nil
}

func GetGoodsTotal(where map[string]interface{}) (int64, error) {
	var count int64
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return 0, errs
	}
	if err := db.Model(&Goods{}).Where(cond, vals...).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

func CleanGoods(where map[string]interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Unscoped().Where(cond, vals...).Delete(&Goods{}).Error; err != nil {
		return err
	}

	return nil
}
